package org.bizming.record.service;

import java.util.List;

import org.bizming.base.BaseException;
import org.bizming.record.dao.RecordTypeDao;
import org.bizming.record.model.RecordType;
import org.bizming.record.model.RecordTypeErrCode;
import org.bizming.util.StringHelper;

public class RecordTypeServiceImpl implements RecordTypeService {

	private RecordTypeDao recordTypeDao;

	private static int LEN_TYPE = 30;

	private static int LEN_DESCRIPTION = 300;

	public boolean add(RecordType recordType) throws BaseException {
		if (!StringHelper.isLengthIn(recordType.getType(), LEN_TYPE)) {
			throw new BaseException(RecordTypeErrCode.TYPE_INVALID);
		}
		if (!StringHelper.isLengthIn(recordType.getDescription(),
				LEN_DESCRIPTION)) {
			throw new BaseException(RecordTypeErrCode.DESCRIPTION_INVALID);
		}
		recordType.setCreateTime(System.currentTimeMillis() / 1000);
		recordType.setActionTime(System.currentTimeMillis() / 1000);
		if (StringHelper.isEmpty(recordType.getUsername())) {
			recordType.setUsername("admin");
		}
		return recordTypeDao.add(recordType);
	}

	public boolean update(RecordType recordType) throws BaseException {
		if (!StringHelper.isLengthIn(recordType.getType(), LEN_TYPE)) {
			throw new BaseException(RecordTypeErrCode.TYPE_INVALID);
		}
		if (!StringHelper.isLengthIn(recordType.getDescription(),
				LEN_DESCRIPTION)) {
			throw new BaseException(RecordTypeErrCode.DESCRIPTION_INVALID);
		}
		recordType.setActionTime(System.currentTimeMillis() / 1000);
		if (StringHelper.isEmpty(recordType.getUsername())) {
			recordType.setUsername("admin");
		}
		return recordTypeDao.update(recordType);
	}

	public RecordType get(int id) {
		return recordTypeDao.get(id);
	}

	public boolean remove(int id) {
		return recordTypeDao.remove(id);
	}

	public List<RecordType> selectList(int page, int pages) {
		return recordTypeDao.selectList((page - 1) * pages, pages);
	}

	public int count() {
		return recordTypeDao.count();
	}

	// =====================================
	public RecordTypeDao getRecordTypeDao() {
		return recordTypeDao;
	}

	public void setRecordTypeDao(RecordTypeDao recordTypeDao) {
		this.recordTypeDao = recordTypeDao;
	}

}
